package com.sri.db.dao.user;

import java.sql.SQLException;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.sri.model.user.ACustomer;
import com.sri.model.user.AGroup;
import com.sri.model.user.User;

public class UserDAOImpl extends SqlMapClientDaoSupport implements UserDAO {

	public boolean addUser(User user) throws SQLException {
		try {
			System.out.println("user Insertion");
			Integer userId = (Integer) getSqlMapClient().insert("addUser", user);
			System.out.println("userId"+userId);
			user.setId(userId);
			getSqlMapClient().insert("addAuthUser", populateAuthCustomer(user));
			getSqlMapClient().insert("addAuthGroup", populateAuthGroup(user));
		} catch (Exception e) {
			throw new SQLException(e.getMessage());
		}
		return true;
	}

	private ACustomer populateAuthCustomer(User user) {
		ACustomer customer = new ACustomer();
		customer.setAuthName(user.getUserName());
		customer.setAuthPass(user.getPassWord());
		customer.setCustId(user.getId());
		return customer;

	}

	private AGroup populateAuthGroup(User user) {
		AGroup group = new AGroup();
		group.setCustAuthName(user.getUserName());
		group.setCustGroup("customers");
		return group;

	}
}
